<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>perlfaq7 - General Perl Language Issues - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/perlfaq7">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlfaq7.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/perlfaq7">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/perlfaq7">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/perlfaq7">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/perlfaq7">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/perlfaq7">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/perlfaq7">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/perlfaq7">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/perlfaq7">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/perlfaq7">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/perlfaq7">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/perlfaq7">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/perlfaq7">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/perlfaq7">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/perlfaq7">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/perlfaq7">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/perlfaq7">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/perlfaq7">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/perlfaq7">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/perlfaq7">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/perlfaq7">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/perlfaq7">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/perlfaq7">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/perlfaq7">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/perlfaq7">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/perlfaq7">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/perlfaq7">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/perlfaq7">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/perlfaq7">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/perlfaq7">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/perlfaq7">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/perlfaq7">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/perlfaq7">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/perlfaq7">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/perlfaq7">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/perlfaq7">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/perlfaq7">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/perlfaq7">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/perlfaq7">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/perlfaq7">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/perlfaq7">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/perlfaq7">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/perlfaq7">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/perlfaq7">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/perlfaq7">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/perlfaq7">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/perlfaq7">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/perlfaq7">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/perlfaq7">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/perlfaq7">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/perlfaq7">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/perlfaq7">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/perlfaq7">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/perlfaq7">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/perlfaq7">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/perlfaq7">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/perlfaq7">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/perlfaq7">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/perlfaq7">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/perlfaq7">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/perlfaq7">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/perlfaq7">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/perlfaq7">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/perlfaq7">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/perlfaq7">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/perlfaq7">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/perlfaq7">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/perlfaq7">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/perlfaq7">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/perlfaq7">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/perlfaq7">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/perlfaq7">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/perlfaq7">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/perlfaq7">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/perlfaq7">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/perlfaq7">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/perlfaq7">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/perlfaq7">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/perlfaq7">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/perlfaq7">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/perlfaq7">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlfaq7.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/perlfaq7">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/perlfaq7">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/perlfaq7">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/perlfaq7">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/perlfaq7">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/perlfaq7">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/perlfaq7">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/perlfaq7">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/perlfaq7">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/perlfaq7">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/perlfaq7">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/perlfaq7">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/perlfaq7">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/perlfaq7">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/perlfaq7">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/perlfaq7">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/perlfaq7">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/perlfaq7">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/perlfaq7">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/perlfaq7">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/perlfaq7">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/perlfaq7">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/perlfaq7">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/perlfaq7">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/perlfaq7">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/perlfaq7">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/perlfaq7">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/perlfaq7">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/perlfaq7">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/perlfaq7">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/perlfaq7">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/perlfaq7">5.35.1</a>
          <a class="dropdown-item active" href="perlfaq7.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="perlfaq7.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="perlfaq7.html">perlfaq7</a>
          <div id="more">
            (<a href="perlfaq7.txt">source</a>,
            <a href="https://metacpan.org/pod/perlfaq7">CPAN</a>)
          </div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#VERSION">VERSION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#DESCRIPTION">DESCRIPTION</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#Can-I-get-a-BNF/yacc/RE-for-the-Perl-language?">Can I get a BNF/yacc/RE for the Perl language?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What-are-all-these-$@%25&amp;*-punctuation-signs,-and-how-do-I-know-when-to-use-them?">What are all these $@%&amp;* punctuation signs, and how do I know when to use them?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#Do-I-always/never-have-to-quote-my-strings-or-use-semicolons-and-commas?">Do I always/never have to quote my strings or use semicolons and commas?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-skip-some-return-values?">How do I skip some return values?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-temporarily-block-warnings?">How do I temporarily block warnings?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What&#39;s-an-extension?">What&#39;s an extension?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#Why-do-Perl-operators-have-different-precedence-than-C-operators?">Why do Perl operators have different precedence than C operators?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-declare/create-a-structure?">How do I declare/create a structure?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-create-a-module?">How do I create a module?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-adopt-or-take-over-a-module-already-on-CPAN?">How do I adopt or take over a module already on CPAN?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-create-a-class?">How do I create a class?  </a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-tell-if-a-variable-is-tainted?">How can I tell if a variable is tainted?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What&#39;s-a-closure?">What&#39;s a closure?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What-is-variable-suicide-and-how-can-I-prevent-it?">What is variable suicide and how can I prevent it?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-pass/return-a-%7BFunction,-FileHandle,-Array,-Hash,-Method,-Regex%7D?">How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-create-a-static-variable?">How do I create a static variable?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What&#39;s-the-difference-between-dynamic-and-lexical-(static)-scoping?-Between-local()-and-my()?">What&#39;s the difference between dynamic and lexical (static) scoping? Between local() and my()?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope?">How can I access a dynamic variable while a similarly named lexical is in scope?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What&#39;s-the-difference-between-deep-and-shallow-binding?">What&#39;s the difference between deep and shallow binding?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#Why-doesn&#39;t-%22my($foo)-=-%3C$fh%3E;%22-work-right?">Why doesn&#39;t &quot;my($foo) = &lt;$fh&gt;;&quot; work right?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-redefine-a-builtin-function,-operator,-or-method?">How do I redefine a builtin function, operator, or method?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What&#39;s-the-difference-between-calling-a-function-as-&amp;foo-and-foo()?">What&#39;s the difference between calling a function as &amp;foo and foo()?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-create-a-switch-or-case-statement?">How do I create a switch or case statement?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-catch-accesses-to-undefined-variables,-functions,-or-methods?">How can I catch accesses to undefined variables, functions, or methods?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#Why-can&#39;t-a-method-included-in-this-same-file-be-found?">Why can&#39;t a method included in this same file be found?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-find-out-my-current-or-calling-package?">How can I find out my current or calling package?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-comment-out-a-large-block-of-Perl-code?">How can I comment out a large block of Perl code?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-do-I-clear-a-package?">How do I clear a package?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#How-can-I-use-a-variable-as-a-variable-name?">How can I use a variable as a variable name?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#What-does-%22bad-interpreter%22-mean?">What does &quot;bad interpreter&quot; mean?</a>
              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library?">Do I need to recompile XS modules when there is a change in the C library?</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="perlfaq7.html#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="perlfaq7.html#NAME">#</a>NAME</h1>

<p>perlfaq7 - General Perl Language Issues</p>

<h1 id="VERSION"><a class="permalink" href="perlfaq7.html#VERSION">#</a>VERSION</h1>

<p>version 5.20210411</p>

<h1 id="DESCRIPTION"><a class="permalink" href="perlfaq7.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>This section deals with general Perl language issues that don&#39;t clearly fit into any of the other sections.</p>

<h2 id="Can-I-get-a-BNF/yacc/RE-for-the-Perl-language?"><a class="permalink" href="perlfaq7.html#Can-I-get-a-BNF/yacc/RE-for-the-Perl-language?">#</a><a id="Can"></a><a id="Can-I-get-a-BNF-yacc-RE-for-the-Perl-language"></a>Can I get a BNF/yacc/RE for the Perl language?</h2>

<p>There is no BNF, but you can paw your way through the yacc grammar in perly.y in the source distribution if you&#39;re particularly brave. The grammar relies on very smart tokenizing code, so be prepared to venture into toke.c as well.</p>

<p>In the words of Chaim Frenkel: &quot;Perl&#39;s grammar can not be reduced to BNF. The work of parsing perl is distributed between yacc, the lexer, smoke and mirrors.&quot;</p>

<h2 id="What-are-all-these-$@%&amp;*-punctuation-signs,-and-how-do-I-know-when-to-use-them?"><a class="permalink" href="perlfaq7.html#What-are-all-these-$@%25&amp;*-punctuation-signs,-and-how-do-I-know-when-to-use-them?">#</a><a id="What"></a><a id="What-are-all-these-punctuation-signs-and-how-do-I-know-when-to-use-them"></a>What are all these $@%&amp;* punctuation signs, and how do I know when to use them?</h2>

<p>They are type specifiers, as detailed in <a href="perldata.html">perldata</a>:</p>

<pre><code class="plaintext">$ for scalar values (number, string or reference)
@ for arrays
% for hashes (associative arrays)
&amp; for subroutines (aka functions, procedures, methods)
* for all types of that symbol name. In version 4 you used them like
  pointers, but in modern perls you can just use references.</code></pre>

<p>There are a couple of other symbols that you&#39;re likely to encounter that aren&#39;t really type specifiers:</p>

<pre><code class="plaintext">&lt;&gt; are used for inputting a record from a filehandle.
\  takes a reference to something.</code></pre>

<p>Note that &lt;FILE&gt; is <i>neither</i> the type specifier for files nor the name of the handle. It is the <code>&lt;&gt;</code> operator applied to the handle FILE. It reads one line (well, record--see <a href="perlvar.html#%24%2F">&quot;$/&quot; in perlvar</a>) from the handle FILE in scalar context, or <i>all</i> lines in list context. When performing open, close, or any other operation besides <code>&lt;&gt;</code> on files, or even when talking about the handle, do <i>not</i> use the brackets. These are correct: <code>eof(FH)</code>, <code>seek(FH, 0, 2)</code> and &quot;copying from STDIN to FILE&quot;.</p>

<h2 id="Do-I-always/never-have-to-quote-my-strings-or-use-semicolons-and-commas?"><a class="permalink" href="perlfaq7.html#Do-I-always/never-have-to-quote-my-strings-or-use-semicolons-and-commas?">#</a><a id="Do"></a><a id="Do-I-always-never-have-to-quote-my-strings-or-use-semicolons-and-commas"></a>Do I always/never have to quote my strings or use semicolons and commas?</h2>

<p>Normally, a bareword doesn&#39;t need to be quoted, but in most cases probably should be (and must be under <code>use strict</code>). But a hash key consisting of a simple word and the left-hand operand to the <code>=&gt;</code> operator both count as though they were quoted:</p>

<pre><code>This                    is like this
------------            ---------------
$foo{line}              $foo{&#39;line&#39;}
bar =&gt; stuff            &#39;bar&#39; =&gt; stuff</code></pre>

<p>The final semicolon in a block is optional, as is the final comma in a list. Good style (see <a href="perlstyle.html">perlstyle</a>) says to put them in except for one-liners:</p>

<pre><code>if ($whoops) { exit 1 }
my @nums = (1, 2, 3);

if ($whoops) {
    exit 1;
}

my @lines = (
    &quot;There Beren came from mountains cold&quot;,
    &quot;And lost he wandered under leaves&quot;,
);</code></pre>

<h2 id="How-do-I-skip-some-return-values?"><a class="permalink" href="perlfaq7.html#How-do-I-skip-some-return-values?">#</a><a id="How"></a><a id="How-do-I-skip-some-return-values"></a>How do I skip some return values?</h2>

<p>One way is to treat the return values as a list and index into it:</p>

<pre><code>$dir = (getpwnam($user))[7];</code></pre>

<p>Another way is to use undef as an element on the left-hand-side:</p>

<pre><code>($dev, $ino, undef, undef, $uid, $gid) = stat($file);</code></pre>

<p>You can also use a list slice to select only the elements that you need:</p>

<pre><code>($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5];</code></pre>

<h2 id="How-do-I-temporarily-block-warnings?"><a class="permalink" href="perlfaq7.html#How-do-I-temporarily-block-warnings?">#</a><a id="How1"></a><a id="How-do-I-temporarily-block-warnings"></a>How do I temporarily block warnings?</h2>

<p>If you are running Perl 5.6.0 or better, the <code>use warnings</code> pragma allows fine control of what warnings are produced. See <a href="perllexwarn.html">perllexwarn</a> for more details.</p>

<pre><code>{
    no warnings;          # temporarily turn off warnings
    $x = $y + $z;         # I know these might be undef
}</code></pre>

<p>Additionally, you can enable and disable categories of warnings. You turn off the categories you want to ignore and you can still get other categories of warnings. See <a href="perllexwarn.html">perllexwarn</a> for the complete details, including the category names and hierarchy.</p>

<pre><code>{
    no warnings &#39;uninitialized&#39;;
    $x = $y + $z;
}</code></pre>

<p>If you have an older version of Perl, the <code>$^W</code> variable (documented in <a href="perlvar.html">perlvar</a>) controls runtime warnings for a block:</p>

<pre><code>{
    local $^W = 0;        # temporarily turn off warnings
    $x = $y + $z;         # I know these might be undef
}</code></pre>

<p>Note that like all the punctuation variables, you cannot currently use my() on <code>$^W</code>, only local().</p>

<h2 id="What&#39;s-an-extension?"><a class="permalink" href="perlfaq7.html#What&#39;s-an-extension?">#</a><a id="What1"></a><a id="Whats-an-extension"></a>What&#39;s an extension?</h2>

<p>An extension is a way of calling compiled C code from Perl. Reading <a href="perlxstut.html">perlxstut</a> is a good place to learn more about extensions.</p>

<h2 id="Why-do-Perl-operators-have-different-precedence-than-C-operators?"><a class="permalink" href="perlfaq7.html#Why-do-Perl-operators-have-different-precedence-than-C-operators?">#</a><a id="Why"></a><a id="Why-do-Perl-operators-have-different-precedence-than-C-operators"></a>Why do Perl operators have different precedence than C operators?</h2>

<p>Actually, they don&#39;t. All C operators that Perl copies have the same precedence in Perl as they do in C. The problem is with operators that C doesn&#39;t have, especially functions that give a list context to everything on their right, eg. print, chmod, exec, and so on. Such functions are called &quot;list operators&quot; and appear as such in the precedence table in <a href="perlop.html">perlop</a>.</p>

<p>A common mistake is to write:</p>

<pre><code>unlink $file || die &quot;snafu&quot;;</code></pre>

<p>This gets interpreted as:</p>

<pre><code>unlink ($file || die &quot;snafu&quot;);</code></pre>

<p>To avoid this problem, either put in extra parentheses or use the super low precedence <code>or</code> operator:</p>

<pre><code>(unlink $file) || die &quot;snafu&quot;;
unlink $file or die &quot;snafu&quot;;</code></pre>

<p>The &quot;English&quot; operators (<code>and</code>, <code>or</code>, <code>xor</code>, and <code>not</code>) deliberately have precedence lower than that of list operators for just such situations as the one above.</p>

<p>Another operator with surprising precedence is exponentiation. It binds more tightly even than unary minus, making <code>-2**2</code> produce a negative four and not a positive one. It is also right-associating, meaning that <code>2**3**2</code> is two raised to the ninth power, not eight squared.</p>

<p>Although it has the same precedence as in C, Perl&#39;s <code>?:</code> operator produces an lvalue. This assigns $x to either $if_true or $if_false, depending on the trueness of $maybe:</p>

<pre><code>($maybe ? $if_true : $if_false) = $x;</code></pre>

<h2 id="How-do-I-declare/create-a-structure?"><a class="permalink" href="perlfaq7.html#How-do-I-declare/create-a-structure?">#</a><a id="How2"></a><a id="How-do-I-declare-create-a-structure"></a>How do I declare/create a structure?</h2>

<p>In general, you don&#39;t &quot;declare&quot; a structure. Just use a (probably anonymous) hash reference. See <a href="perlref.html">perlref</a> and <a href="perldsc.html">perldsc</a> for details. Here&#39;s an example:</p>

<pre><code>$person = {};                   # new anonymous hash
$person-&gt;{AGE}  = 24;           # set field AGE to 24
$person-&gt;{NAME} = &quot;Nat&quot;;        # set field NAME to &quot;Nat&quot;</code></pre>

<p>If you&#39;re looking for something a bit more rigorous, try <a href="perlootut.html">perlootut</a>.</p>

<h2 id="How-do-I-create-a-module?"><a class="permalink" href="perlfaq7.html#How-do-I-create-a-module?">#</a><a id="How3"></a><a id="How-do-I-create-a-module"></a>How do I create a module?</h2>

<p><a href="perlnewmod.html">perlnewmod</a> is a good place to start, ignore the bits about uploading to CPAN if you don&#39;t want to make your module publicly available.</p>

<p><a href="./ExtUtils::ModuleMaker.html">ExtUtils::ModuleMaker</a> and <a href="./Module::Starter.html">Module::Starter</a> are also good places to start. Many CPAN authors now use <a href="./Dist::Zilla.html">Dist::Zilla</a> to automate as much as possible.</p>

<p>Detailed documentation about modules can be found at: <a href="perlmod.html">perlmod</a>, <a href="perlmodlib.html">perlmodlib</a>, <a href="perlmodstyle.html">perlmodstyle</a>.</p>

<p>If you need to include C code or C library interfaces use h2xs. h2xs will create the module distribution structure and the initial interface files. <a href="perlxs.html">perlxs</a> and <a href="perlxstut.html">perlxstut</a> explain the details.</p>

<h2 id="How-do-I-adopt-or-take-over-a-module-already-on-CPAN?"><a class="permalink" href="perlfaq7.html#How-do-I-adopt-or-take-over-a-module-already-on-CPAN?">#</a><a id="How4"></a><a id="How-do-I-adopt-or-take-over-a-module-already-on-CPAN"></a>How do I adopt or take over a module already on CPAN?</h2>

<p>Ask the current maintainer to make you a co-maintainer or transfer the module to you.</p>

<p>If you can not reach the author for some reason contact the PAUSE admins at modules@perl.org who may be able to help, but each case is treated separately.</p>

<ul>

<li><p>Get a login for the Perl Authors Upload Server (PAUSE) if you don&#39;t already have one: <a href="http://pause.perl.org">http://pause.perl.org</a></p>

</li>
<li><p>Write to modules@perl.org explaining what you did to contact the current maintainer. The PAUSE admins will also try to reach the maintainer.</p>

</li>
<li><p>Post a public message in a heavily trafficked site announcing your intention to take over the module.</p>

</li>
<li><p>Wait a bit. The PAUSE admins don&#39;t want to act too quickly in case the current maintainer is on holiday. If there&#39;s no response to private communication or the public post, a PAUSE admin can transfer it to you.</p>

</li>
</ul>

<h2 id="How-do-I-create-a-class?"><a class="permalink" href="perlfaq7.html#How-do-I-create-a-class?">#</a><a id="How5"></a><a id="How-do-I-create-a-class"></a>How do I create a class?  </h2>

<p>(contributed by brian d foy)</p>

<p>In Perl, a class is just a package, and methods are just subroutines. Perl doesn&#39;t get more formal than that and lets you set up the package just the way that you like it (that is, it doesn&#39;t set up anything for you).</p>

<p>See also <a href="perlootut.html">perlootut</a>, a tutorial that covers class creation, and <a href="perlobj.html">perlobj</a>.</p>

<h2 id="How-can-I-tell-if-a-variable-is-tainted?"><a class="permalink" href="perlfaq7.html#How-can-I-tell-if-a-variable-is-tainted?">#</a><a id="How6"></a><a id="How-can-I-tell-if-a-variable-is-tainted"></a>How can I tell if a variable is tainted?</h2>

<p>You can use the tainted() function of the Scalar::Util module, available from CPAN (or included with Perl since release 5.8.0). See also <a href="perlsec.html#Laundering-and-Detecting-Tainted-Data">&quot;Laundering and Detecting Tainted Data&quot; in perlsec</a>.</p>

<h2 id="What&#39;s-a-closure?"><a class="permalink" href="perlfaq7.html#What&#39;s-a-closure?">#</a><a id="What2"></a><a id="Whats-a-closure"></a>What&#39;s a closure?</h2>

<p>Closures are documented in <a href="perlref.html">perlref</a>.</p>

<p><i>Closure</i> is a computer science term with a precise but hard-to-explain meaning. Usually, closures are implemented in Perl as anonymous subroutines with lasting references to lexical variables outside their own scopes. These lexicals magically refer to the variables that were around when the subroutine was defined (deep binding).</p>

<p>Closures are most often used in programming languages where you can have the return value of a function be itself a function, as you can in Perl. Note that some languages provide anonymous functions but are not capable of providing proper closures: the Python language, for example. For more information on closures, check out any textbook on functional programming. Scheme is a language that not only supports but encourages closures.</p>

<p>Here&#39;s a classic non-closure function-generating function:</p>

<pre><code>sub add_function_generator {
    return sub { shift() + shift() };
}

my $add_sub = add_function_generator();
my $sum = $add_sub-&gt;(4,5);                # $sum is 9 now.</code></pre>

<p>The anonymous subroutine returned by add_function_generator() isn&#39;t technically a closure because it refers to no lexicals outside its own scope. Using a closure gives you a <i>function template</i> with some customization slots left out to be filled later.</p>

<p>Contrast this with the following make_adder() function, in which the returned anonymous function contains a reference to a lexical variable outside the scope of that function itself. Such a reference requires that Perl return a proper closure, thus locking in for all time the value that the lexical had when the function was created.</p>

<pre><code>sub make_adder {
    my $addpiece = shift;
    return sub { shift() + $addpiece };
}

my $f1 = make_adder(20);
my $f2 = make_adder(555);</code></pre>

<p>Now <code>$f1-&gt;($n)</code> is always 20 plus whatever $n you pass in, whereas <code>$f2-&gt;($n)</code> is always 555 plus whatever $n you pass in. The $addpiece in the closure sticks around.</p>

<p>Closures are often used for less esoteric purposes. For example, when you want to pass in a bit of code into a function:</p>

<pre><code>my $line;
timeout( 30, sub { $line = &lt;STDIN&gt; } );</code></pre>

<p>If the code to execute had been passed in as a string, <code>&#39;$line = &lt;STDIN&gt;&#39;</code>, there would have been no way for the hypothetical timeout() function to access the lexical variable $line back in its caller&#39;s scope.</p>

<p>Another use for a closure is to make a variable <i>private</i> to a named subroutine, e.g. a counter that gets initialized at creation time of the sub and can only be modified from within the sub. This is sometimes used with a BEGIN block in package files to make sure a variable doesn&#39;t get meddled with during the lifetime of the package:</p>

<pre><code>BEGIN {
    my $id = 0;
    sub next_id { ++$id }
}</code></pre>

<p>This is discussed in more detail in <a href="perlsub.html">perlsub</a>; see the entry on <i>Persistent Private Variables</i>.</p>

<h2 id="What-is-variable-suicide-and-how-can-I-prevent-it?"><a class="permalink" href="perlfaq7.html#What-is-variable-suicide-and-how-can-I-prevent-it?">#</a><a id="What3"></a><a id="What-is-variable-suicide-and-how-can-I-prevent-it"></a>What is variable suicide and how can I prevent it?</h2>

<p>This problem was fixed in perl 5.004_05, so preventing it means upgrading your version of perl. ;)</p>

<p>Variable suicide is when you (temporarily or permanently) lose the value of a variable. It is caused by scoping through my() and local() interacting with either closures or aliased foreach() iterator variables and subroutine arguments. It used to be easy to inadvertently lose a variable&#39;s value this way, but now it&#39;s much harder. Take this code:</p>

<pre><code>my $f = &#39;foo&#39;;
sub T {
    while ($i++ &lt; 3) { my $f = $f; $f .= &quot;bar&quot;; print $f, &quot;\n&quot; }
}

T;
print &quot;Finally $f\n&quot;;</code></pre>

<p>If you are experiencing variable suicide, that <code>my $f</code> in the subroutine doesn&#39;t pick up a fresh copy of the <code>$f</code> whose value is <code>&#39;foo&#39;</code>. The output shows that inside the subroutine the value of <code>$f</code> leaks through when it shouldn&#39;t, as in this output:</p>

<pre><code class="plaintext">foobar
foobarbar
foobarbarbar
Finally foo</code></pre>

<p>The $f that has &quot;bar&quot; added to it three times should be a new <code>$f</code> <code>my $f</code> should create a new lexical variable each time through the loop. The expected output is:</p>

<pre><code class="plaintext">foobar
foobar
foobar
Finally foo</code></pre>

<h2 id="How-can-I-pass/return-a-{Function,-FileHandle,-Array,-Hash,-Method,-Regex}?"><a class="permalink" href="perlfaq7.html#How-can-I-pass/return-a-%7BFunction,-FileHandle,-Array,-Hash,-Method,-Regex%7D?">#</a><a id="How7"></a><a id="How-can-I-pass-return-a-Function-FileHandle-Array-Hash-Method-Regex"></a>How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?</h2>

<p>You need to pass references to these objects. See <a href="perlsub.html#Pass-by-Reference">&quot;Pass by Reference&quot; in perlsub</a> for this particular question, and <a href="perlref.html">perlref</a> for information on references.</p>

<dl>

<dt id="Passing-Variables-and-Functions"><a class="permalink" href="perlfaq7.html#Passing-Variables-and-Functions">#</a><a id="Passing"></a>Passing Variables and Functions</dt>
<dd>

<p>Regular variables and functions are quite easy to pass: just pass in a reference to an existing or anonymous variable or function:</p>

<pre><code>func( \$some_scalar );

func( \@some_array  );
func( [ 1 .. 10 ]   );

func( \%some_hash   );
func( { this =&gt; 10, that =&gt; 20 }   );

func( \&amp;some_func   );
func( sub { $_[0] ** $_[1] }   );</code></pre>

</dd>
<dt id="Passing-Filehandles"><a class="permalink" href="perlfaq7.html#Passing-Filehandles">#</a><a id="Passing1"></a>Passing Filehandles</dt>
<dd>

<p>As of Perl 5.6, you can represent filehandles with scalar variables which you treat as any other scalar.</p>

<pre><code>open my $fh, $filename or die &quot;Cannot open $filename! $!&quot;;
func( $fh );

sub func {
    my $passed_fh = shift;

    my $line = &lt;$passed_fh&gt;;
}</code></pre>

<p>Before Perl 5.6, you had to use the <code>*FH</code> or <code>\*FH</code> notations. These are &quot;typeglobs&quot;--see <a href="perldata.html#Typeglobs-and-Filehandles">&quot;Typeglobs and Filehandles&quot; in perldata</a> and especially <a href="perlsub.html#Pass-by-Reference">&quot;Pass by Reference&quot; in perlsub</a> for more information.</p>

</dd>
<dt id="Passing-Regexes"><a class="permalink" href="perlfaq7.html#Passing-Regexes">#</a><a id="Passing2"></a>Passing Regexes</dt>
<dd>

<p>Here&#39;s an example of how to pass in a string and a regular expression for it to match against. You construct the pattern with the <code>qr//</code> operator:</p>

<pre><code>sub compare {
    my ($val1, $regex) = @_;
    my $retval = $val1 =~ /$regex/;
    return $retval;
}
$match = compare(&quot;old McDonald&quot;, qr/d.*D/i);</code></pre>

</dd>
<dt id="Passing-Methods"><a class="permalink" href="perlfaq7.html#Passing-Methods">#</a><a id="Passing3"></a>Passing Methods</dt>
<dd>

<p>To pass an object method into a subroutine, you can do this:</p>

<pre><code>call_a_lot(10, $some_obj, &quot;methname&quot;)
sub call_a_lot {
    my ($count, $widget, $trick) = @_;
    for (my $i = 0; $i &lt; $count; $i++) {
        $widget-&gt;$trick();
    }
}</code></pre>

<p>Or, you can use a closure to bundle up the object, its method call, and arguments:</p>

<pre><code>my $whatnot = sub { $some_obj-&gt;obfuscate(@args) };
func($whatnot);
sub func {
    my $code = shift;
    &amp;$code();
}</code></pre>

<p>You could also investigate the can() method in the UNIVERSAL class (part of the standard perl distribution).</p>

</dd>
</dl>

<h2 id="How-do-I-create-a-static-variable?"><a class="permalink" href="perlfaq7.html#How-do-I-create-a-static-variable?">#</a><a id="How8"></a><a id="How-do-I-create-a-static-variable"></a>How do I create a static variable?</h2>

<p>(contributed by brian d foy)</p>

<p>In Perl 5.10, declare the variable with <code>state</code>. The <code>state</code> declaration creates the lexical variable that persists between calls to the subroutine:</p>

<pre><code>sub counter { state $count = 1; $count++ }</code></pre>

<p>You can fake a static variable by using a lexical variable which goes out of scope. In this example, you define the subroutine <code>counter</code>, and it uses the lexical variable <code>$count</code>. Since you wrap this in a BEGIN block, <code>$count</code> is defined at compile-time, but also goes out of scope at the end of the BEGIN block. The BEGIN block also ensures that the subroutine and the value it uses is defined at compile-time so the subroutine is ready to use just like any other subroutine, and you can put this code in the same place as other subroutines in the program text (i.e. at the end of the code, typically). The subroutine <code>counter</code> still has a reference to the data, and is the only way you can access the value (and each time you do, you increment the value). The data in chunk of memory defined by <code>$count</code> is private to <code>counter</code>.</p>

<pre><code>BEGIN {
    my $count = 1;
    sub counter { $count++ }
}

my $start = counter();

.... # code that calls counter();

my $end = counter();</code></pre>

<p>In the previous example, you created a function-private variable because only one function remembered its reference. You could define multiple functions while the variable is in scope, and each function can share the &quot;private&quot; variable. It&#39;s not really &quot;static&quot; because you can access it outside the function while the lexical variable is in scope, and even create references to it. In this example, <code>increment_count</code> and <code>return_count</code> share the variable. One function adds to the value and the other simply returns the value. They can both access <code>$count</code>, and since it has gone out of scope, there is no other way to access it.</p>

<pre><code>BEGIN {
    my $count = 1;
    sub increment_count { $count++ }
    sub return_count    { $count }
}</code></pre>

<p>To declare a file-private variable, you still use a lexical variable. A file is also a scope, so a lexical variable defined in the file cannot be seen from any other file.</p>

<p>See <a href="perlsub.html#Persistent-Private-Variables">&quot;Persistent Private Variables&quot; in perlsub</a> for more information. The discussion of closures in <a href="perlref.html">perlref</a> may help you even though we did not use anonymous subroutines in this answer. See <a href="perlsub.html#Persistent-Private-Variables">&quot;Persistent Private Variables&quot; in perlsub</a> for details.</p>

<h2 id="What&#39;s-the-difference-between-dynamic-and-lexical-(static)-scoping?-Between-local()-and-my()?"><a class="permalink" href="perlfaq7.html#What&#39;s-the-difference-between-dynamic-and-lexical-(static)-scoping?-Between-local()-and-my()?">#</a><a id="What4"></a><a id="Whats-the-difference-between-dynamic-and-lexical-static-scoping-Between-local-and-my"></a>What&#39;s the difference between dynamic and lexical (static) scoping? Between local() and my()?</h2>

<p><code>local($x)</code> saves away the old value of the global variable <code>$x</code> and assigns a new value for the duration of the subroutine <i>which is visible in other functions called from that subroutine</i>. This is done at run-time, so is called dynamic scoping. local() always affects global variables, also called package variables or dynamic variables.</p>

<p><code>my($x)</code> creates a new variable that is only visible in the current subroutine. This is done at compile-time, so it is called lexical or static scoping. my() always affects private variables, also called lexical variables or (improperly) static(ly scoped) variables.</p>

<p>For instance:</p>

<pre><code>sub visible {
    print &quot;var has value $var\n&quot;;
}

sub dynamic {
    local $var = &#39;local&#39;;    # new temporary value for the still-global
    visible();              #   variable called $var
}

sub lexical {
    my $var = &#39;private&#39;;    # new private variable, $var
    visible();              # (invisible outside of sub scope)
}

$var = &#39;global&#39;;

visible();              # prints global
dynamic();              # prints local
lexical();              # prints global</code></pre>

<p>Notice how at no point does the value &quot;private&quot; get printed. That&#39;s because $var only has that value within the block of the lexical() function, and it is hidden from the called subroutine.</p>

<p>In summary, local() doesn&#39;t make what you think of as private, local variables. It gives a global variable a temporary value. my() is what you&#39;re looking for if you want private variables.</p>

<p>See <a href="perlsub.html#Private-Variables-via-my%28%29">&quot;Private Variables via my()&quot; in perlsub</a> and <a href="perlsub.html#Temporary-Values-via-local%28%29">&quot;Temporary Values via local()&quot; in perlsub</a> for excruciating details.</p>

<h2 id="How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope?"><a class="permalink" href="perlfaq7.html#How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope?">#</a><a id="How9"></a><a id="How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope"></a>How can I access a dynamic variable while a similarly named lexical is in scope?</h2>

<p>If you know your package, you can just mention it explicitly, as in $Some_Pack::var. Note that the notation $::var is <b>not</b> the dynamic $var in the current package, but rather the one in the &quot;main&quot; package, as though you had written $main::var.</p>

<pre><code>use vars &#39;$var&#39;;
local $var = &quot;global&quot;;
my    $var = &quot;lexical&quot;;

print &quot;lexical is $var\n&quot;;
print &quot;global  is $main::var\n&quot;;</code></pre>

<p>Alternatively you can use the compiler directive our() to bring a dynamic variable into the current lexical scope.</p>

<pre><code>require 5.006; # our() did not exist before 5.6
use vars &#39;$var&#39;;

local $var = &quot;global&quot;;
my $var    = &quot;lexical&quot;;

print &quot;lexical is $var\n&quot;;

{
    our $var;
    print &quot;global  is $var\n&quot;;
}</code></pre>

<h2 id="What&#39;s-the-difference-between-deep-and-shallow-binding?"><a class="permalink" href="perlfaq7.html#What&#39;s-the-difference-between-deep-and-shallow-binding?">#</a><a id="What5"></a><a id="Whats-the-difference-between-deep-and-shallow-binding"></a>What&#39;s the difference between deep and shallow binding?</h2>

<p>In deep binding, lexical variables mentioned in anonymous subroutines are the same ones that were in scope when the subroutine was created. In shallow binding, they are whichever variables with the same names happen to be in scope when the subroutine is called. Perl always uses deep binding of lexical variables (i.e., those created with my()). However, dynamic variables (aka global, local, or package variables) are effectively shallowly bound. Consider this just one more reason not to use them. See the answer to <a href="perlfaq7.html#What%27s-a-closure%3F">&quot;What&#39;s a closure?&quot;</a>.</p>

<h2 id="Why-doesn&#39;t-&quot;my($foo)-=-&lt;$fh&gt;;&quot;-work-right?"><a class="permalink" href="perlfaq7.html#Why-doesn&#39;t-%22my($foo)-=-%3C$fh%3E;%22-work-right?">#</a><a id="Why1"></a><a id="Why-doesnt-my-foo-fh-work-right"></a>Why doesn&#39;t &quot;my($foo) = &lt;$fh&gt;;&quot; work right?</h2>

<p><code>my()</code> and <code>local()</code> give list context to the right hand side of <code>=</code>. The &lt;$fh&gt; read operation, like so many of Perl&#39;s functions and operators, can tell which context it was called in and behaves appropriately. In general, the scalar() function can help. This function does nothing to the data itself (contrary to popular myth) but rather tells its argument to behave in whatever its scalar fashion is. If that function doesn&#39;t have a defined scalar behavior, this of course doesn&#39;t help you (such as with sort()).</p>

<p>To enforce scalar context in this particular case, however, you need merely omit the parentheses:</p>

<pre><code>local($foo) = &lt;$fh&gt;;        # WRONG
local($foo) = scalar(&lt;$fh&gt;);   # ok
local $foo  = &lt;$fh&gt;;        # right</code></pre>

<p>You should probably be using lexical variables anyway, although the issue is the same here:</p>

<pre><code>my($foo) = &lt;$fh&gt;;    # WRONG
my $foo  = &lt;$fh&gt;;    # right</code></pre>

<h2 id="How-do-I-redefine-a-builtin-function,-operator,-or-method?"><a class="permalink" href="perlfaq7.html#How-do-I-redefine-a-builtin-function,-operator,-or-method?">#</a><a id="How10"></a><a id="How-do-I-redefine-a-builtin-function-operator-or-method"></a>How do I redefine a builtin function, operator, or method?</h2>

<p>Why do you want to do that? :-)</p>

<p>If you want to override a predefined function, such as open(), then you&#39;ll have to import the new definition from a different module. See <a href="perlsub.html#Overriding-Built-in-Functions">&quot;Overriding Built-in Functions&quot; in perlsub</a>.</p>

<p>If you want to overload a Perl operator, such as <code>+</code> or <code>**</code>, then you&#39;ll want to use the <code>use overload</code> pragma, documented in <a href="overload.html">overload</a>.</p>

<p>If you&#39;re talking about obscuring method calls in parent classes, see <a href="perlootut.html#Overriding-methods-and-method-resolution">&quot;Overriding methods and method resolution&quot; in perlootut</a>.</p>

<h2 id="What&#39;s-the-difference-between-calling-a-function-as-&amp;foo-and-foo()?"><a class="permalink" href="perlfaq7.html#What&#39;s-the-difference-between-calling-a-function-as-&amp;foo-and-foo()?">#</a><a id="What6"></a><a id="Whats-the-difference-between-calling-a-function-as-foo-and-foo"></a>What&#39;s the difference between calling a function as &amp;foo and foo()?</h2>

<p>(contributed by brian d foy)</p>

<p>Calling a subroutine as <code>&amp;foo</code> with no trailing parentheses ignores the prototype of <code>foo</code> and passes it the current value of the argument list, <code>@_</code>. Here&#39;s an example; the <code>bar</code> subroutine calls <code>&amp;foo</code>, which prints its arguments list:</p>

<pre><code>sub foo { print &quot;Args in foo are: @_\n&quot;; }

sub bar { &amp;foo; }

bar( &quot;a&quot;, &quot;b&quot;, &quot;c&quot; );</code></pre>

<p>When you call <code>bar</code> with arguments, you see that <code>foo</code> got the same <code>@_</code>:</p>

<pre><code class="plaintext">Args in foo are: a b c</code></pre>

<p>Calling the subroutine with trailing parentheses, with or without arguments, does not use the current <code>@_</code>. Changing the example to put parentheses after the call to <code>foo</code> changes the program:</p>

<pre><code>sub foo { print &quot;Args in foo are: @_\n&quot;; }

sub bar { &amp;foo(); }

bar( &quot;a&quot;, &quot;b&quot;, &quot;c&quot; );</code></pre>

<p>Now the output shows that <code>foo</code> doesn&#39;t get the <code>@_</code> from its caller.</p>

<pre><code class="plaintext">Args in foo are:</code></pre>

<p>However, using <code>&amp;</code> in the call still overrides the prototype of <code>foo</code> if present:</p>

<pre><code>sub foo ($$$) { print &quot;Args infoo are: @_\n&quot;; }

sub bar_1 { &amp;foo; }
sub bar_2 { &amp;foo(); }
sub bar_3 { foo( $_[0], $_[1], $_[2] ); }
# sub bar_4 { foo(); }
# bar_4 doesn&#39;t compile: &quot;Not enough arguments for main::foo at ...&quot;

bar_1( &quot;a&quot;, &quot;b&quot;, &quot;c&quot; );
# Args in foo are: a b c

bar_2( &quot;a&quot;, &quot;b&quot;, &quot;c&quot; );
# Args in foo are:

bar_3( &quot;a&quot;, &quot;b&quot;, &quot;c&quot; );
# Args in foo are: a b c</code></pre>

<p>The main use of the <code>@_</code> pass-through feature is to write subroutines whose main job it is to call other subroutines for you. For further details, see <a href="perlsub.html">perlsub</a>.</p>

<h2 id="How-do-I-create-a-switch-or-case-statement?"><a class="permalink" href="perlfaq7.html#How-do-I-create-a-switch-or-case-statement?">#</a><a id="How11"></a><a id="How-do-I-create-a-switch-or-case-statement"></a>How do I create a switch or case statement?</h2>

<p>There is a given/when statement in Perl, but it is experimental and likely to change in future. See <a href="perlsyn.html">perlsyn</a> for more details.</p>

<p>The general answer is to use a CPAN module such as <a href="./Switch::Plain.html">Switch::Plain</a>:</p>

<pre><code>use Switch::Plain;
sswitch($variable_holding_a_string) {
    case &#39;first&#39;: { }
    case &#39;second&#39;: { }
    default: { }
}</code></pre>

<p>or for more complicated comparisons, <code>if-elsif-else</code>:</p>

<pre><code>for ($variable_to_test) {
    if    (/pat1/)  { }     # do something
    elsif (/pat2/)  { }     # do something else
    elsif (/pat3/)  { }     # do something else
    else            { }     # default
}</code></pre>

<p>Here&#39;s a simple example of a switch based on pattern matching, lined up in a way to make it look more like a switch statement. We&#39;ll do a multiway conditional based on the type of reference stored in $whatchamacallit:</p>

<pre><code>SWITCH: for (ref $whatchamacallit) {

    /^$/           &amp;&amp; die &quot;not a reference&quot;;

    /SCALAR/       &amp;&amp; do {
                    print_scalar($$ref);
                    last SWITCH;
                  };

    /ARRAY/        &amp;&amp; do {
                    print_array(@$ref);
                    last SWITCH;
                  };

    /HASH/        &amp;&amp; do {
                    print_hash(%$ref);
                    last SWITCH;
                  };

    /CODE/        &amp;&amp; do {
                    warn &quot;can&#39;t print function ref&quot;;
                    last SWITCH;
                  };

    # DEFAULT

    warn &quot;User defined type skipped&quot;;

}</code></pre>

<p>See <a href="perlsyn.html">perlsyn</a> for other examples in this style.</p>

<p>Sometimes you should change the positions of the constant and the variable. For example, let&#39;s say you wanted to test which of many answers you were given, but in a case-insensitive way that also allows abbreviations. You can use the following technique if the strings all start with different characters or if you want to arrange the matches so that one takes precedence over another, as <code>&quot;SEND&quot;</code> has precedence over <code>&quot;STOP&quot;</code> here:</p>

<pre><code>chomp($answer = &lt;&gt;);
if    (&quot;SEND&quot;  =~ /^\Q$answer/i) { print &quot;Action is send\n&quot;  }
elsif (&quot;STOP&quot;  =~ /^\Q$answer/i) { print &quot;Action is stop\n&quot;  }
elsif (&quot;ABORT&quot; =~ /^\Q$answer/i) { print &quot;Action is abort\n&quot; }
elsif (&quot;LIST&quot;  =~ /^\Q$answer/i) { print &quot;Action is list\n&quot;  }
elsif (&quot;EDIT&quot;  =~ /^\Q$answer/i) { print &quot;Action is edit\n&quot;  }</code></pre>

<p>A totally different approach is to create a hash of function references.</p>

<pre><code>my %commands = (
    &quot;happy&quot; =&gt; \&amp;joy,
    &quot;sad&quot;,  =&gt; \&amp;sullen,
    &quot;done&quot;  =&gt; sub { die &quot;See ya!&quot; },
    &quot;mad&quot;   =&gt; \&amp;angry,
);

print &quot;How are you? &quot;;
chomp($string = &lt;STDIN&gt;);
if ($commands{$string}) {
    $commands{$string}-&gt;();
} else {
    print &quot;No such command: $string\n&quot;;
}</code></pre>

<p>Starting from Perl 5.8, a source filter module, <code>Switch</code>, can also be used to get switch and case. Its use is now discouraged, because it&#39;s not fully compatible with the native switch of Perl 5.10, and because, as it&#39;s implemented as a source filter, it doesn&#39;t always work as intended when complex syntax is involved.</p>

<h2 id="How-can-I-catch-accesses-to-undefined-variables,-functions,-or-methods?"><a class="permalink" href="perlfaq7.html#How-can-I-catch-accesses-to-undefined-variables,-functions,-or-methods?">#</a><a id="How12"></a><a id="How-can-I-catch-accesses-to-undefined-variables-functions-or-methods"></a>How can I catch accesses to undefined variables, functions, or methods?</h2>

<p>The AUTOLOAD method, discussed in <a href="perlsub.html#Autoloading">&quot;Autoloading&quot; in perlsub</a> lets you capture calls to undefined functions and methods.</p>

<p>When it comes to undefined variables that would trigger a warning under <code>use warnings</code>, you can promote the warning to an error.</p>

<pre><code>use warnings FATAL =&gt; qw(uninitialized);</code></pre>

<h2 id="Why-can&#39;t-a-method-included-in-this-same-file-be-found?"><a class="permalink" href="perlfaq7.html#Why-can&#39;t-a-method-included-in-this-same-file-be-found?">#</a><a id="Why2"></a><a id="Why-cant-a-method-included-in-this-same-file-be-found"></a>Why can&#39;t a method included in this same file be found?</h2>

<p>Some possible reasons: your inheritance is getting confused, you&#39;ve misspelled the method name, or the object is of the wrong type. Check out <a href="perlootut.html">perlootut</a> for details about any of the above cases. You may also use <code>print ref($object)</code> to find out the class <code>$object</code> was blessed into.</p>

<p>Another possible reason for problems is that you&#39;ve used the indirect object syntax (eg, <code>find Guru &quot;Samy&quot;</code>) on a class name before Perl has seen that such a package exists. It&#39;s wisest to make sure your packages are all defined before you start using them, which will be taken care of if you use the <code>use</code> statement instead of <code>require</code>. If not, make sure to use arrow notation (eg., <code>Guru-&gt;find(&quot;Samy&quot;)</code>) instead. Object notation is explained in <a href="perlobj.html">perlobj</a>.</p>

<p>Make sure to read about creating modules in <a href="perlmod.html">perlmod</a> and the perils of indirect objects in <a href="perlobj.html#Method-Invocation">&quot;Method Invocation&quot; in perlobj</a>.</p>

<h2 id="How-can-I-find-out-my-current-or-calling-package?"><a class="permalink" href="perlfaq7.html#How-can-I-find-out-my-current-or-calling-package?">#</a><a id="How13"></a><a id="How-can-I-find-out-my-current-or-calling-package"></a>How can I find out my current or calling package?</h2>

<p>(contributed by brian d foy)</p>

<p>To find the package you are currently in, use the special literal <code>__PACKAGE__</code>, as documented in <a href="perldata.html">perldata</a>. You can only use the special literals as separate tokens, so you can&#39;t interpolate them into strings like you can with variables:</p>

<pre><code>my $current_package = __PACKAGE__;
print &quot;I am in package $current_package\n&quot;;</code></pre>

<p>If you want to find the package calling your code, perhaps to give better diagnostics as <a href="Carp.html">Carp</a> does, use the <code>caller</code> built-in:</p>

<pre><code>sub foo {
    my @args = ...;
    my( $package, $filename, $line ) = caller;

    print &quot;I was called from package $package\n&quot;;
    );</code></pre>

<p>By default, your program starts in package <code>main</code>, so you will always be in some package.</p>

<p>This is different from finding out the package an object is blessed into, which might not be the current package. For that, use <code>blessed</code> from <a href="./Scalar::Util.html">Scalar::Util</a>, part of the Standard Library since Perl 5.8:</p>

<pre><code>use Scalar::Util qw(blessed);
my $object_package = blessed( $object );</code></pre>

<p>Most of the time, you shouldn&#39;t care what package an object is blessed into, however, as long as it claims to inherit from that class:</p>

<pre><code>my $is_right_class = eval { $object-&gt;isa( $package ) }; # true or false</code></pre>

<p>And, with Perl 5.10 and later, you don&#39;t have to check for an inheritance to see if the object can handle a role. For that, you can use <code>DOES</code>, which comes from <code>UNIVERSAL</code>:</p>

<pre><code>my $class_does_it = eval { $object-&gt;DOES( $role ) }; # true or false</code></pre>

<p>You can safely replace <code>isa</code> with <code>DOES</code> (although the converse is not true).</p>

<h2 id="How-can-I-comment-out-a-large-block-of-Perl-code?"><a class="permalink" href="perlfaq7.html#How-can-I-comment-out-a-large-block-of-Perl-code?">#</a><a id="How14"></a><a id="How-can-I-comment-out-a-large-block-of-Perl-code"></a>How can I comment out a large block of Perl code?</h2>

<p>(contributed by brian d foy)</p>

<p>The quick-and-dirty way to comment out more than one line of Perl is to surround those lines with Pod directives. You have to put these directives at the beginning of the line and somewhere where Perl expects a new statement (so not in the middle of statements like the <code>#</code> comments). You end the comment with <code>=cut</code>, ending the Pod section:</p>

<pre><code>=pod

my $object = NotGonnaHappen-&gt;new();

ignored_sub();

$wont_be_assigned = 37;

=cut</code></pre>

<p>The quick-and-dirty method only works well when you don&#39;t plan to leave the commented code in the source. If a Pod parser comes along, your multiline comment is going to show up in the Pod translation. A better way hides it from Pod parsers as well.</p>

<p>The <code>=begin</code> directive can mark a section for a particular purpose. If the Pod parser doesn&#39;t want to handle it, it just ignores it. Label the comments with <code>comment</code>. End the comment using <code>=end</code> with the same label. You still need the <code>=cut</code> to go back to Perl code from the Pod comment:</p>

<pre><code>=begin comment

my $object = NotGonnaHappen-&gt;new();

ignored_sub();

$wont_be_assigned = 37;

=end comment

=cut</code></pre>

<p>For more information on Pod, check out <a href="perlpod.html">perlpod</a> and <a href="perlpodspec.html">perlpodspec</a>.</p>

<h2 id="How-do-I-clear-a-package?"><a class="permalink" href="perlfaq7.html#How-do-I-clear-a-package?">#</a><a id="How15"></a><a id="How-do-I-clear-a-package"></a>How do I clear a package?</h2>

<p>Use this code, provided by Mark-Jason Dominus:</p>

<pre><code>sub scrub_package {
    no strict &#39;refs&#39;;
    my $pack = shift;
    die &quot;Shouldn&#39;t delete main package&quot;
        if $pack eq &quot;&quot; || $pack eq &quot;main&quot;;
    my $stash = *{$pack . &#39;::&#39;}{HASH};
    my $name;
    foreach $name (keys %$stash) {
        my $fullname = $pack . &#39;::&#39; . $name;
        # Get rid of everything with that name.
        undef $$fullname;
        undef @$fullname;
        undef %$fullname;
        undef &amp;$fullname;
        undef *$fullname;
    }
}</code></pre>

<p>Or, if you&#39;re using a recent release of Perl, you can just use the Symbol::delete_package() function instead.</p>

<h2 id="How-can-I-use-a-variable-as-a-variable-name?"><a class="permalink" href="perlfaq7.html#How-can-I-use-a-variable-as-a-variable-name?">#</a><a id="How16"></a><a id="How-can-I-use-a-variable-as-a-variable-name"></a>How can I use a variable as a variable name?</h2>

<p>Beginners often think they want to have a variable contain the name of a variable.</p>

<pre><code>$fred    = 23;
$varname = &quot;fred&quot;;
++$$varname;         # $fred now 24</code></pre>

<p>This works <i>sometimes</i>, but it is a very bad idea for two reasons.</p>

<p>The first reason is that this technique <i>only works on global variables</i>. That means that if $fred is a lexical variable created with my() in the above example, the code wouldn&#39;t work at all: you&#39;d accidentally access the global and skip right over the private lexical altogether. Global variables are bad because they can easily collide accidentally and in general make for non-scalable and confusing code.</p>

<p>Symbolic references are forbidden under the <code>use strict</code> pragma. They are not true references and consequently are not reference-counted or garbage-collected.</p>

<p>The other reason why using a variable to hold the name of another variable is a bad idea is that the question often stems from a lack of understanding of Perl data structures, particularly hashes. By using symbolic references, you are just using the package&#39;s symbol-table hash (like <code>%main::</code>) instead of a user-defined hash. The solution is to use your own hash or a real reference instead.</p>

<pre><code>$USER_VARS{&quot;fred&quot;} = 23;
my $varname = &quot;fred&quot;;
$USER_VARS{$varname}++;  # not $$varname++</code></pre>

<p>There we&#39;re using the %USER_VARS hash instead of symbolic references. Sometimes this comes up in reading strings from the user with variable references and wanting to expand them to the values of your perl program&#39;s variables. This is also a bad idea because it conflates the program-addressable namespace and the user-addressable one. Instead of reading a string and expanding it to the actual contents of your program&#39;s own variables:</p>

<pre><code>$str = &#39;this has a $fred and $barney in it&#39;;
$str =~ s/(\$\w+)/$1/eeg;          # need double eval</code></pre>

<p>it would be better to keep a hash around like %USER_VARS and have variable references actually refer to entries in that hash:</p>

<pre><code>$str =~ s/\$(\w+)/$USER_VARS{$1}/g;   # no /e here at all</code></pre>

<p>That&#39;s faster, cleaner, and safer than the previous approach. Of course, you don&#39;t need to use a dollar sign. You could use your own scheme to make it less confusing, like bracketed percent symbols, etc.</p>

<pre><code>$str = &#39;this has a %fred% and %barney% in it&#39;;
$str =~ s/%(\w+)%/$USER_VARS{$1}/g;   # no /e here at all</code></pre>

<p>Another reason that folks sometimes think they want a variable to contain the name of a variable is that they don&#39;t know how to build proper data structures using hashes. For example, let&#39;s say they wanted two hashes in their program: %fred and %barney, and that they wanted to use another scalar variable to refer to those by name.</p>

<pre><code>$name = &quot;fred&quot;;
$$name{WIFE} = &quot;wilma&quot;;     # set %fred

$name = &quot;barney&quot;;
$$name{WIFE} = &quot;betty&quot;;    # set %barney</code></pre>

<p>This is still a symbolic reference, and is still saddled with the problems enumerated above. It would be far better to write:</p>

<pre><code>$folks{&quot;fred&quot;}{WIFE}   = &quot;wilma&quot;;
$folks{&quot;barney&quot;}{WIFE} = &quot;betty&quot;;</code></pre>

<p>And just use a multilevel hash to start with.</p>

<p>The only times that you absolutely <i>must</i> use symbolic references are when you really must refer to the symbol table. This may be because it&#39;s something that one can&#39;t take a real reference to, such as a format name. Doing so may also be important for method calls, since these always go through the symbol table for resolution.</p>

<p>In those cases, you would turn off <code>strict &#39;refs&#39;</code> temporarily so you can play around with the symbol table. For example:</p>

<pre><code>@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
    no strict &#39;refs&#39;;  # renege for the block
    *$name = sub { &quot;&lt;FONT COLOR=&#39;$name&#39;&gt;@_&lt;/FONT&gt;&quot; };
}</code></pre>

<p>All those functions (red(), blue(), green(), etc.) appear to be separate, but the real code in the closure actually was compiled only once.</p>

<p>So, sometimes you might want to use symbolic references to manipulate the symbol table directly. This doesn&#39;t matter for formats, handles, and subroutines, because they are always global--you can&#39;t use my() on them. For scalars, arrays, and hashes, though--and usually for subroutines-- you probably only want to use hard references.</p>

<h2 id="What-does-&quot;bad-interpreter&quot;-mean?"><a class="permalink" href="perlfaq7.html#What-does-%22bad-interpreter%22-mean?">#</a><a id="What7"></a><a id="What-does-bad-interpreter-mean"></a>What does &quot;bad interpreter&quot; mean?</h2>

<p>(contributed by brian d foy)</p>

<p>The &quot;bad interpreter&quot; message comes from the shell, not perl. The actual message may vary depending on your platform, shell, and locale settings.</p>

<p>If you see &quot;bad interpreter - no such file or directory&quot;, the first line in your perl script (the &quot;shebang&quot; line) does not contain the right path to perl (or any other program capable of running scripts). Sometimes this happens when you move the script from one machine to another and each machine has a different path to perl--/usr/bin/perl versus /usr/local/bin/perl for instance. It may also indicate that the source machine has CRLF line terminators and the destination machine has LF only: the shell tries to find /usr/bin/perl&lt;CR&gt;, but can&#39;t.</p>

<p>If you see &quot;bad interpreter: Permission denied&quot;, you need to make your script executable.</p>

<p>In either case, you should still be able to run the scripts with perl explicitly:</p>

<pre><code class="plaintext">% perl script.pl</code></pre>

<p>If you get a message like &quot;perl: command not found&quot;, perl is not in your PATH, which might also mean that the location of perl is not where you expect it so you need to adjust your shebang line.</p>

<h2 id="Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library?"><a class="permalink" href="perlfaq7.html#Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library?">#</a><a id="Do1"></a><a id="Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library"></a>Do I need to recompile XS modules when there is a change in the C library?</h2>

<p>(contributed by Alex Beamish)</p>

<p>If the new version of the C library is ABI-compatible (that&#39;s Application Binary Interface compatible) with the version you&#39;re upgrading from, and if the shared library version didn&#39;t change, no re-compilation should be necessary.</p>

<h1 id="AUTHOR-AND-COPYRIGHT"><a class="permalink" href="perlfaq7.html#AUTHOR-AND-COPYRIGHT">#</a><a id="AUTHOR"></a>AUTHOR AND COPYRIGHT</h1>

<p>Copyright (c) 1997-2013 Tom Christiansen, Nathan Torkington, and other authors as noted. All rights reserved.</p>

<p>This documentation is free; you can redistribute it and/or modify it under the same terms as Perl itself.</p>

<p>Irrespective of its distribution, all code examples in this file are hereby placed into the public domain. You are permitted and encouraged to use this code in your own programs for fun or for profit as you see fit. A simple comment in the code giving credit would be courteous but is not required.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
